package org.hank.compose_yyzs.ui.widget.dialog

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import org.hank.compose_yyzs.R
import org.hank.compose_yyzs.ui.theme.AppTheme
import org.hank.compose_yyzs.utils.getString
import org.hank.compose_yyzs.utils.getStringGlobal

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/8/15 18:26
 *描述: 登录多商户选择弹框
 */

@Composable
fun BottomListSelectDialog(
    title: String,
    state: MutableState<Boolean>,
    listItems: List<String>,
    navigator: DestinationsNavigator,
    defaultSelect:Int = -1,
    onItemClick: (position: Int) -> Unit,
    onCancelClick: () -> Unit,
) {
    var selectIndex by remember { mutableIntStateOf(defaultSelect) }
        DialogLikeBottomSheet(
            dismissRequest = {
                onCancelClick.invoke()
            },
            dragHandle = null,
            showState = state,
        ) {
            Spacer(modifier = Modifier.height(16.dp))
            Column {
                Text(
                    text = title,
                    style = MaterialTheme.typography.titleMedium.copy(),
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(bottom = 16.dp)
                        .fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
                Divider(color = AppTheme.colors.divider, thickness = 1.dp)
                listItems.forEach { item ->
                    SelectListItem(
                        itemName = item,
                        listItems.indexOf(item) == selectIndex
                    ) {
                        selectIndex = listItems.indexOf(item)
                    }
                }
                Spacer(modifier = Modifier.height(4.dp))
                ActionDialogCompose({
                    onCancelClick.invoke()
                }) {
                    if (selectIndex != -1)
                        onItemClick.invoke(selectIndex)
                    else {
                        navigator.navigate(
                            org.hank.compose_yyzs.ui.destinations.TipsDialogDestination(
                                getStringGlobal(R.string.login_company_selected_hint)
                            )
                        )
                    }
                }
                Spacer(modifier = Modifier.height(12.dp))
            }
        }
}

@Composable
fun SelectListItem(itemName: String, selected: Boolean = false, onItemSelect: () -> Unit) {

    Column(modifier = Modifier
        .fillMaxWidth()
        .clickable {
            onItemSelect.invoke()
        }) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            Text(text = itemName, modifier = Modifier.weight(1f))
            if (selected)
                Image(
                    painter = painterResource(id = R.mipmap.ic_radio_btn_checked),
                    contentDescription = ""
                )
        }
        Divider(thickness = 1.dp, color = AppTheme.colors.divider)
    }
}

@Composable
fun ActionDialogCompose(onCancelClick: () -> Unit = {}, onConfirmClick: () -> Unit = {}) {
    Row(modifier = Modifier.fillMaxWidth()) {
        Spacer(modifier = Modifier.width(16.dp))
        Button(
            onClick = { onCancelClick.invoke() },
            colors = ButtonDefaults.buttonColors(containerColor = AppTheme.colors.secondBtnBg),
            shape = MaterialTheme.shapes.medium,
            modifier = Modifier
                .weight(1f)
                .padding(top = 16.dp, bottom = 16.dp)
        ) {
            Text(
                text = getString(R.string.cancel),
                color = MaterialTheme.colorScheme.onBackground,
                style = MaterialTheme.typography.bodyLarge
            )
        }
        Spacer(modifier = Modifier.width(16.dp))
        Button(
            onClick = { onConfirmClick.invoke() },
            shape = MaterialTheme.shapes.medium,
            modifier = Modifier
                .weight(1f)
                .padding(top = 16.dp, bottom = 16.dp)
        ) {
            Text(text = getString(R.string.confirm), style = MaterialTheme.typography.bodyLarge)
        }
        Spacer(modifier = Modifier.width(16.dp))
    }
}